﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using LantinxLBS.Common;
using LantinxShare.LBS;
using LantinxShare.Common;
using System.Text;

namespace LantinxLBS.Views
{
    public partial class FrmTask : ChildWindow
    {
        public FrmTask()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(FrmTask_Loaded);
        }

        void FrmTask_Loaded(object sender, RoutedEventArgs e)
        {
            GetUsers();
            GetMarks();
            GetWorkItemType();
            this.dateStart.SelectedDate = DateTime.Now;
            dateEnd.SelectedDate = DateTime.Now.AddDays(1);
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            AddTask();

            
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void AddTask()
        {
            GUser user = null;
            GMark mark = null;
            bool isFalse = false;
            //取得任务的起止时间,设置默认的任务状态 为默认
            DateTime startDate = this.dateStart.SelectedDate.Value;
            DateTime endDate = this.dateEnd.SelectedDate.Value;

            if (this.dateStart.SelectedDate == null)
            {
                MsgHelper.Show("请选择一个日期");
                return;
            }

            if (this.dateEnd.SelectedDate == null)
            {
                MsgHelper.Show("请选择一个日期");
                return;
            }

            if (startDate >= endDate)
            {
                isFalse = true;
                MsgHelper.Show("开始时间要小于结束时间");
                return;
            }
            //设置一个用户
            if (this.cmbUser.SelectedIndex > -1)
            {
                user = this.cmbUser.SelectedValue as GUser;
            }
            else
            {
                isFalse = true;
                MsgHelper.Show("请选择一个用户");
                return;
            }
            //选择一个标注
            if (this.cmbMark.SelectedIndex > -1)
            {
                mark = this.cmbMark.SelectedValue as GMark;
            }else{
                isFalse = true;
                MsgHelper.Show("请选择一个标注");
                return;
            }
            StringBuilder builder = new StringBuilder();
            //选中工作项
            if (checkSet.Count > 0)
            {

                foreach (var item in checkSet)
                {
                    if (item.Value)
                    {
                        builder.Append(item.Key);
                        builder.Append(",");
                    }
                }

            }
            else
            {
                isFalse = true;
                MsgHelper.Show("请选择任务类型");
                return;
            }

            if (!isFalse)
            {
                DataHelper.SaveTask(user, mark, startDate, endDate, builder.ToString().TrimEnd(','), (result) =>
                {

                    this.Dispatcher.BeginInvoke(() =>
                    {
                        this.DialogResult = true;
                    });
                });
            }
        }

        private void GetUsers()
        {
            DataHelper.LoadUser((result) =>
            {

                this.Dispatcher.BeginInvoke(() =>
                {
                    this.cmbUser.ItemsSource = result;
                    //.ItemsSource = ;
                });
            });

        }
        private void GetMarks()
        {
            DataHelper.LoadMark((result) =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    this.cmbMark.ItemsSource = result;
                });
            });
        }
        private void GetWorkItemType()
        {
            DataHelper.LoadWorkItemType((result) =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    this.lstWorkItemType.ItemsSource = result;
                });
            });
        }
        private Dictionary<int, bool> checkSet = new Dictionary<int, bool>();
        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            var chkBox = sender as CheckBox;
            if (chkBox.Tag as GWorkItemType != null)
            {
                GWorkItemType workItem = chkBox.Tag as GWorkItemType;
                checkSet[workItem.Id] = chkBox.IsChecked.Value;
            }
        }
    }
}

